<?php
/**
 * Created by PhpStorm.
 * User: zengyuming
 * Date: 2017/8/12
 * Time: 下午5:34
 */

namespace Admin\Model;
use Think\Model;

class transactionModel extends Model {
    public function sumPayMoney(){
        $Model = $this;
        $SumPayMoney = $Model->field("sum(paymoney) as Sumpaymoney , pay_type")->where(array('pay_status'=>1))->group('pay_type')->select();
        return transactionModel::ArrReturn($SumPayMoney);
    }

    public function daysPayMoney($t = 'Today'){
        if($t == 'Today') {
            // 今天
            $StartTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
            $EndTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        }elseif($t  == 'Yesterday') {
            // 昨天
            $StartTime = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
            $EndTime = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
        }elseif($t == 'thismonth') {
            // 本月
            $StartTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
            $EndTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        }elseif($t == 'WeekThisPaymoney'){
            // 本周  7天内
            $now = time();
            $time = '1' == date('w') ? strtotime('Monday', $now) : strtotime('last Monday', $now);
            $StartTime = strtotime(date('Y-m-d 00:00:00', $time));
            $EndTime = strtotime(date('Y-m-d 23:59:59', strtotime('Sunday', $now)));
        }

        $Model = $this;
        $where = array(
            'pay_status' => 1,
            'update_begtime' => array(array("EGT" ,  $StartTime), array('ELT',$EndTime))
        );
        $SumpayData = $Model->field("pay_type,sum(paymoney) as Sumpaymoney")->where($where)->group('pay_type')->select();
        return transactionModel::ArrReturn($SumpayData);
    }

    /* 拿出的是分组之后的数据 然后在进行处理每个不同的支付类型并且返回 */
    private static function ArrReturn($SumpayData = array()){
        foreach($SumpayData as $val){
            switch (BankType($val['pay_type'])){
                case 'WeixinScancode':
                    $WeixinScancode = $val['sumpaymoney'];
                    break;
                case 'qq_pay':
                    $qq_pay = $val['sumpaymoney'];
                    break;
                case 'alipay_pay':
                    $alipay_pay = $val['sumpaymoney'];
                    break;
                case 'weixinGzh_pay':
                    $weixinGzh_pay = $val['sumpaymoney'];
                    break;
                case 'jd_pay':
                    $jd_pay = $val['sumpaymoney'];
                    break;
                case 'bank_pay':
                    $bank_pay = $val['sumpaymoney'];
                    break;
            }
        }
        $data = array(
            'qq_pay' => isset($qq_pay) ? $qq_pay : 0.000,
            'alipay_pay' => isset($alipay_pay) ? $alipay_pay : 0.000,
            'weixinGzh_pay' => isset($weixinGzh_pay) ? $weixinGzh_pay : 0.000,
            'WeixinScancode' => isset($WeixinScancode) ? $WeixinScancode : 0.000,
            'jd_pay' => isset($jd_pay) ? $jd_pay : 0.000,
            'bank_pay' => isset($bank_pay) ? $bank_pay : 0.000
        );
        return $data;
    }

    public function transactionSearch(){
        $order_number = I("request.order_number");
        $merchant_id = I("request.merchant_id");
        $pay_type = I("request.pay_type");
        $startTime = urldecode(I("request.startTime"));
        $EndTime = urldecode(I("request.EndTime"));
        $price = intval(I("request.price"));
        $Order_Status = $_GET['order_status'];
        $where = array();
        if($Order_Status == "1") {
            $where['pay_status'] = array('eq' , 1);
        }elseif($Order_Status == "0"){
            $where['pay_status'] = array('eq' , 0);
        }

        $passageway = explode("@" , I('get.passageway'));
        if($order_number!='') $where['out_trade_no'] = array('eq' , $order_number);
        if($merchant_id != '') $where['merchant'] = array('eq' , $merchant_id);
        if($pay_type != '') $where['pay_type'] = array('eq' , $pay_type);
        if($price != 0) $where['paymoney'] = array('eq' , $price);
        if($passageway[1] != '') $where['pay_type'] = array('eq',$passageway[1]);
        if($passageway[0] != '') $where['mch_type'] = array('eq',$passageway[0]);
        if(isset($startTime) && $startTime != 0 && isset($EndTime) && $EndTime != 0){
            $where['create_begtime'] = array(array("EGT" ,  strtotime($startTime)), array('ELT',strtotime($EndTime)));
        }
        return $where;
    }

    public function successrate(){
//        $Data = $this
//            ->alias("a")
//            ->field("b.id , a.mch_type , b.asname ,(select count(*) from zym_transaction where pay_status = 0 and mch_type = a.mch_type ) as fail_num , (select count(*) from zym_transaction where pay_status = 1 and mch_type = a.mch_type) as success_num")
//            ->join('left join zym_payinfo as b on a.mch_type = b.Pay_MethodName')
//            ->group('mch_type')->select();

//        select CASE b.pay_type
//     WHEN 'WeixinScancode' THEN '微信扫码'
//     WHEN 'qq_pay' THEN 'QQ支付'
//     WHEN 'alipay_pay' THEN '支付宝'
//     WHEN 'weixinGzh_pay' THEN '微信公众号'
//     END as chname , count(*) as success_num , b.asname as asname , a.mch_type , (select count(*) as fail_num from zym_transaction where create_begtime >= 1504454400  and pay_status = 0 and mch_type = a.mch_type) as fail_num from zym_transaction as a left join zym_payinfo as b on a.mch_type = b.Pay_MethodName where mch_type <> '' and create_begtime >= 1504454400 and pay_status = 1 group by mch_type
//        $day = strtotime(date("Y-m-d"));

        // 20分钟内的支付成功率
        $day = time() - (20*60);
        $Endday = time();

        // $day = 1513733400;
        // $Endday = 1513734900;


        $SuccessData = $this
            ->alias("a")
            ->field("CASE a.pay_type
    WHEN 'WeixinScancode' THEN '微信扫码'
    WHEN 'qq_pay' THEN 'QQ支付'
    WHEN 'alipay_pay' THEN '支付宝'
    WHEN 'weixinGzh_pay' THEN '微信公众号'
    WHEN 'bank_pay' THEN '网银支付'
    WHEN 'jd_pay' THEN '京东支付'
    END as chname , count(*) as success_num , b.asname as asname , a.mch_type")
            ->join("left join zym_payinfo as b on a.mch_type = b.Pay_MethodName")
            ->where(array('create_begtime' => array('EGT' , $day), array('create_begtime' => array('ELT' , $Endday)) ,array('a.mch_type' => array('neq' , "")) ,array('a.pay_status' => array('eq' , 1))))
            ->group('a.mch_type,a.pay_type')
            ->order('a.create_begtime')
            ->select();

        $FailData = $this
            ->alias("a")
            ->field("CASE a.pay_type
    WHEN 'WeixinScancode' THEN '微信扫码'
    WHEN 'qq_pay' THEN 'QQ支付'
    WHEN 'alipay_pay' THEN '支付宝'
    WHEN 'weixinGzh_pay' THEN '微信公众号'
    WHEN 'bank_pay' THEN '网银支付'
    WHEN 'jd_pay' THEN '京东支付'
    END as chname , count(*) as fail_num , b.asname as asname , a.mch_type")
            ->join("left join zym_payinfo as b on a.mch_type = b.Pay_MethodName")
            ->where(array('create_begtime' => array('EGT' , $day), array('create_begtime' => array('ELT' , $Endday)) , array('a.mch_type' => array('neq' , "")) ,array('a.pay_status' => array('eq' , 0))))
            ->group('a.mch_type,a.pay_type')
            ->order('a.create_begtime')
            ->select();

        //  dump($SuccessData);
        //  dump($FailData);
        // exit;


        $SuccessRateConf = array();


        foreach($FailData as $key => $Value){
            foreach($SuccessData as $key1 => $value1){
                if($Value['mch_type'] == $value1['mch_type'] && $Value['chname'] == $value1['chname']){
                    $cname = $Value['asname'] . "(" . $Value['chname'] . ")";
                    $sumpen = (float)$value1['success_num'] + (float)$Value['fail_num'];
                    $rate = ceil(($value1['success_num'] / $sumpen) * 100);
                    $SuccessRateConf[] = array(
                        'rate' => $rate,
                        'passagewayName' => $cname
                    );
                    unset($FailData[$key]);
                    unset($SuccessData[$key1]);
                }
            }
        }

        foreach($SuccessData as $val1) $SuccessRateConf[] = array('rate' => 100,'passagewayName' => $val1['asname'] . "(" . $val1['chname'] . ")"); // 百分百成功的
        foreach($FailData as $val1) $SuccessRateConf[] = array('rate' => 0,'passagewayName' => $val1['asname'] . "(" . $val1['chname'] . ")"); // 没有成功过的
        // dump($SuccessRateConf);die;
        return $SuccessRateConf;
    }

}